import asyncio
import time
import os
from common.third_party.save_config import load_config, save_config


class OneShotPatcher:
    __slots__ = ['_patches']

    def __init__(self):
        self._patches: list[str] = load_config('pch/one_shot_patches', list[str], [])

    def patch(self, uuid: str, patch, *args, **kwargs):
        if uuid not in self._patches:
            patch(*args, **kwargs)
            self._patches.append(uuid)
            save_config('pch/one_shot_patches', self._patches)

    def reboot_patch(self, uuid: str):
        if uuid not in self._patches:
            self._patches.append(uuid)
            save_config('pch/one_shot_patches', self._patches)
            time.sleep(6)
            os.system("reboot")

    def patch_late(self, loop: asyncio.AbstractEventLoop, sleep_time: int, uuid: str, patch, *args, **kwargs):
        async def _patch():
            await asyncio.sleep(sleep_time)
            await patch(*args, **kwargs)
            self._patches.append(uuid)
            save_config('pch/one_shot_patches', self._patches)

        if uuid not in self._patches:
            loop.create_task(_patch())


